import {myMath} from "./mathUtils.js";
import formattingMoney from "./formattingMoney.js"
/**
 * @param {Array} dataLists | 需要格式化的数据 | [{ date:"2020-12-11",fuel_oil:232…… }……]
 * @param {Array} addField | 需要叠加的字段 | ['fuel_oil',……]
 * 
 * 根据日期（"2020-12-21"）格式化数据为下列格式
 * filterData = {"xx年xx日":{}}
 * mTotalData = {"xx年xx日":123456}
 */
export default function(dataLists, addField) {
	let mTotalData = {};
	let filterData = {};

	//计算单日各项金额总计
	dataLists.map(item => {
		let addRes = 0;
		addField.forEach((key, index) => {
			addRes = myMath.accAdd(addRes, parseFloat(item[key]))
		})
		item.total = formattingMoney(addRes);
		return item;
	})

	//格式化日期，按日期分组
	let saveFilterData = {};
	dataLists.forEach(item => {
		let d = new Date(item.date);
		let y = d.getFullYear();
		let m = d.getMonth() + 1;
		let day = d.getDate();
		m = m < 10 ? `0${m}` : m;
		let yAndM = `${y}年${m}月`;
		item.mAndDay = `${m}月${day}日`;
		if (!saveFilterData[yAndM]) {
			saveFilterData[yAndM] = [];
			mTotalData[yAndM] = 0;
			saveFilterData[yAndM].push(item);
		} else {
			saveFilterData[yAndM].push(item);
		}
	})
	filterData = saveFilterData;

	//计算当月资金总计
	for (let key in saveFilterData) {
		let addRes = 0;
		saveFilterData[key].forEach(item => {
			addRes = myMath.accAdd(addRes, parseFloat(item.total));
		})
		mTotalData[key] = formattingMoney(addRes);
	}

	return {
		mTotalData,
		filterData
	}
}
